package com.ruoyi.manage.service.impl;

import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.ruoyi.manage.mapper.OrderInfoMapper;
import com.ruoyi.manage.service.IAliPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : 赵志冲
 * @date : 2024-07-03 18:56
 * @description :
 **/
@Service
public class AliPayServiceImpl implements IAliPayService {

    @Autowired
    OrderInfoMapper orderInfoMapper;

    @Autowired
    AlipayClient alipayClient;
    @Override
    public String aliPay(String orderId, Integer amount) {
        try {
            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            request.setReturnUrl("http://localhost:18085/manage/pay/callback");
            AlipayTradePagePayModel model = new AlipayTradePagePayModel();
            model.setOutTradeNo(orderId);
            model.setTotalAmount(String.valueOf(amount));
            model.setSubject("Iphone6 16G");
            model.setProductCode("FAST_INSTANT_TRADE_PAY");
            request.setBizModel(model);
            AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "POST");
            // 如果需要返回GET请求，请使用
            // AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "GET");
            String pageRedirectionData = response.getBody();
            System.out.println(pageRedirectionData);
            if (response.isSuccess()) {
                System.out.println("调用成功");
                return pageRedirectionData;
            } else {
                System.out.println("调用失败");
                return "调用失败";
                // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
                // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
                // System.out.println(diagnosisUrl);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}
